//
//  HTCitiesTools.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/12.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCitiesTools.h"
#import "HTAreaModel.h"

@interface HTCitiesTools ()
//所有plist的数据
@property (nonatomic,strong) NSArray *allProvinceData;
//当前省的index
@property (nonatomic,assign) NSInteger currentCityIndex;
//当前省的数据
@property (nonatomic,strong) NSDictionary *allCityData;

@property (nonatomic,strong) NSMutableArray *provinces;//省
@property (nonatomic,strong) NSMutableArray *cities;//市
@property (nonatomic,strong) NSMutableArray *areas;//区

@end;

static HTCitiesTools *instance = nil;
@implementation HTCitiesTools

+ (instancetype)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class]alloc]init];
    });
    return instance;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

//加载省的列表
- (NSArray *)loadProvincesData{
    [self.provinces removeAllObjects];
    [self.provinces addObject:[HTAreaModel areaModelWithTitle:@"全国" areaType:HTAreaTypeNo]];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
    self.allProvinceData = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in self.allProvinceData) {
        if ([dic[@"type"] isEqualToString:@"直辖市"]) {
            
            [self.provinces addObject:[HTAreaModel areaModelWithTitle:dic[@"state"] areaType:HTAreaTypeDirectCity]];
        }else{
            [self.provinces addObject:[HTAreaModel areaModelWithTitle:dic[@"state"] areaType:HTAreaTypeProvince]];
        }
    }
    return self.provinces;
}
//加载市的列表,如果是直辖市，就不加载
- (NSArray *)loadCitiesDataWithIndex:(NSInteger)index{
    [self.cities removeAllObjects];
    NSDictionary *dic = self.allProvinceData[index];
    self.allCityData = dic;
    self.currentCityIndex = index;
    if ([dic[@"type"] isEqualToString:@"直辖市"]) {
        return self.cities;
    }else{
        HTAreaModel *model = [HTAreaModel areaModelWithTitle: [NSString stringWithFormat:@"%@",dic[@"state"]] areaType:HTAreaTypeNo];
        model.provinceString = [NSString stringWithFormat:@"%@",dic[@"state"]];
        [self.cities addObject:model];
        for (NSDictionary *dicCities in dic[@"cities"]) {
            HTAreaModel *model =  [HTAreaModel areaModelWithTitle:dicCities[@"city"] areaType:HTAreaTypeCity];
            model.provinceString =[NSString stringWithFormat:@"%@",dic[@"state"]];
            [self.cities addObject:model];
        }
        return self.cities;
    }
}
//加载区的列表
- (NSArray *)loadAreasDataWithIndex:(NSInteger)index{
    [self.areas removeAllObjects];
    if (self.allCityData == nil) {//永远不会执行
        return self.areas;
    }
    if ([self.allCityData[@"type"] isEqualToString:@"直辖市"]) {
        HTAreaModel *model = [HTAreaModel areaModelWithTitle: [NSString stringWithFormat:@"%@",self.allCityData[@"state"]] areaType:HTAreaTypeNo];
        model.cityString = [NSString stringWithFormat:@"%@",self.allCityData[@"state"]];
        [self.areas addObject:model];
        for (NSDictionary *dicCities in self.allCityData[@"cities"]) {
            HTAreaModel *model = [HTAreaModel areaModelWithTitle:dicCities[@"city"] areaType:HTAreaTypeArea];
            model.cityString = [NSString stringWithFormat:@"%@",self.allCityData[@"state"]];
            [self.areas addObject:model];
        }
    }else{
        NSDictionary *cityDic = self.allCityData[@"cities"][index]; //衡水
        HTAreaModel *model = [HTAreaModel areaModelWithTitle: [NSString stringWithFormat:@"%@",cityDic[@"city"]] areaType:HTAreaTypeNo];
        model.cityString = [NSString stringWithFormat:@"%@",cityDic[@"city"]];
        model.provinceString =  model.provinceString = [NSString stringWithFormat:@"%@",self.allProvinceData[_currentCityIndex][@"state"]];
        [self.areas addObject: model];
        for (NSString *areaStr in cityDic[@"areas"]) {
            HTAreaModel *model = [HTAreaModel areaModelWithTitle:areaStr areaType:HTAreaTypeArea];
             model.cityString = [NSString stringWithFormat:@"%@",cityDic[@"city"]];
             model.provinceString =  model.provinceString = [NSString stringWithFormat:@"%@",self.allProvinceData[_currentCityIndex][@"state"]];
            [self.areas addObject:model];
        }
    }
    return self.areas;
}

//省市区
- (NSMutableArray *)provinces{
    if (!_provinces) {
        _provinces = [NSMutableArray array];
    }
    return _provinces;
}
- (NSMutableArray *)cities{
    if (!_cities) {
        _cities = [NSMutableArray array];
    }
    return _cities;
}
- (NSMutableArray *)areas{
    if (!_areas) {
        _areas = [NSMutableArray array];
    }
    return _areas;
}

@end
